﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabMS.Web.Equipment.Instrument
{
    public partial class instrumentDetailInfo : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYYQGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            strPageUserType.Add("student");

            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            strID = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
            {

                ExtendModule.InstrumentAndConsumables instrument = checkQueryString(strID);
                if (instrument == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "仪器不存在!");
                    return;
                }
                bindDDL(instrument);
                bind(instrument);

                lbPageName.Text = instrument.Name + "详情";
                lbCID.Text = instrument.ID.ToString();
            }
            this.Page.Title = lbPageName.Text;

        }

        ExtendModule.InstrumentAndConsumables checkQueryString(string strID)
        {
            ExtendBLL.InstrumentAndConsumables bconsum = new ExtendBLL.InstrumentAndConsumables();
            string strWhere = " Type = 'yiqi' and ID =" + strID;
            List<ExtendModule.InstrumentAndConsumables> listinstrument = bconsum.GetExtendModelList(strWhere);
            if (listinstrument.Count <= 0)
            {
                return null;
            }
            return listinstrument[0];
        }

        public void bindDDL(ExtendModule.InstrumentAndConsumables mconsum)
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            ddlCClass.DataSource = listCClass;
            ddlCClass.DataTextField = "Name";
            ddlCClass.DataValueField = "Code";
            ddlCClass.DataBind();
            ddlCClass.SelectedValue = mconsum.ClassNumber;

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlComeFrom.DataSource = listCClass;
            ddlComeFrom.DataTextField = "Name";
            ddlComeFrom.DataValueField = "Code";
            ddlComeFrom.DataBind();
            ddlComeFrom.SelectedValue = mconsum.Source;


            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();
            ddlArrow.SelectedValue = mconsum.Arrow;

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();
            ddlFundsSubject.SelectedValue = mconsum.FundsSubject;

            listCClass = eDictionary.GetItemListByDClass("dangwei");
            ddlModle.DataSource = listCClass;
            ddlModle.DataTextField = "Name";
            ddlModle.DataValueField = "Code";
            ddlModle.DataBind();
            ddlModle.SelectedValue = mconsum.Measurement;

            tbLocation.Text = mconsum.LabName;
        }

        void bind(ExtendModule.InstrumentAndConsumables mconsum)
        {
            EquipPhoto.ImageUrl = "~/Equipment/PicShow.aspx?ID=" + mconsum.ID;
            lbClassNub.Text = mconsum.ClassNumber.ToString();
            lbCName.Text = mconsum.Name;
            lbCNub.Text = mconsum.Numbers;
            lbCountry.Text = mconsum.CountryCode;

            lbDocuments.Text = mconsum.Documents.ToString();
            lbFNub.Text = mconsum.FactoryCode.ToString();
            lbNub.Text = mconsum.Quantity.ToString();
            lbPrice.Text = mconsum.UnitPrice;
            lbPurpose.Text = mconsum.Purpose;
            try
            {
                lbProduceDate.Text = mconsum.ProductionDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                lbCreatDate.Text = mconsum.CreatedDate.Value.ToShortDateString();
            }
            catch { }
            try
            {
                lbBuyDate.Text = mconsum.PurchaseDate.Value.ToShortDateString();
            }
            catch { }


            lbTypeNub.Text = mconsum.Model;
            lbVENDOR.Text = "";//应加上
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {

            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            try
            {
                bconsum.Delete(long.Parse(lbCID.Text));
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "删除成功!", "InstrumentPreview.aspx");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
            }


        }
    }
}
